package org.dfl.plugin.views.palette.model;

import org.apache.log4j.Logger;
import org.dfl.core.model.transitions.TavernaTransition;
import org.dfl.core.model.transitions.Transition;
import org.dfl.core.utils.Utils;
import org.dfl.plugin.editor.model.TransitionModelElement;
import org.dfl.plugin.views.palette.model.DefaultPaletteEntry;
import org.eclipse.gef.requests.CreationFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.embl.ebi.escience.scufl.Processor;
import org.embl.ebi.escience.scufl.ScuflModel;
import org.embl.ebi.escience.scuflworkers.ProcessorFactory;

/**
 * entry that represent transition creation tool
 * @author Piotr Wlodarczyk
 *
 */
public class TransitionPaletteEntry extends DefaultPaletteEntry {
	private static Logger logger = Logger.getLogger(TransitionPaletteEntry.class);
	
	String description = null;
	private ProcessorFactory factory;

	@SuppressWarnings("unchecked")
	public TransitionPaletteEntry(final Class clazz){
		super( getTransitionName(clazz), "icons/processors/local.png",
			new CreationFactory(){
			public Object getNewObject() {
				try {
					Transition t = (Transition) clazz.newInstance();
					return new TransitionModelElement(t);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					return null;
				}
			}
			public Object getObjectType() {
				return TransitionModelElement.class;
			}},false);
		
		try {
			Transition t = (Transition) clazz.newInstance();
			description = t.getDescription();
		} catch (Exception e) {}
	}

	public TransitionPaletteEntry(final ProcessorFactory factory, ImageDescriptor imageDescriptor){
		super(getName(factory), imageDescriptor,
				new CreationFactory(){
					public Object getNewObject() {
						try {
							TavernaTransition t = new TavernaTransition(factory);
							return new TransitionModelElement(t);
						} catch (Exception e) {
							// nie powinno sie zdarzyc!
							logger.error(e.getMessage(), e);
							return null;
						}
					}

					public Object getObjectType() {
						return TransitionModelElement.class;
					}},false);
		this.factory = factory;
	}
	

	@Override
	public String getLabel() {
		return super.getLabel()+((description==null || description.equals(""))?"":" ("+description+")");
	}
	
	public void fetchDescription(){
		if(factory!=null && description==null){
			try {
				Processor p = factory.createProcessor("null",new ScuflModel());
				description = Utils.stripTags(p.getDescription());
			} catch (Exception e) {}
			if(description==null || description.equals("")){
				description = factory.getProcessorDescription();
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	private static String getTransitionName(Class clazz){
		return Utils.stripTags(Utils.getClassName(clazz).replaceFirst("Transition$", ""));
	}
	
	private static String getName(final ProcessorFactory factory){
		return Utils.stripTags(factory.getName());
	}

	public String getDescription() {
		return description;
	}
}
